<template>
  <div class="login_container">
    <div class="login_box">
      <div class="avatar_box">
        <img src="../assets/images/logo-big.png"
             alt="" />
      </div>

      <el-form status-icon
               class="login_form"
               :model="loginForm"
               :rules="loginFormRules"
               ref="loginLoginRef">
        <el-form-item prop="account">
          <el-input type="text"
                    auto-complete="off"
                    placeholder="请输入用户名"
                    prefix-icon="el-icon-user"
                    v-model="loginForm.account"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input type="password"
                    auto-complete="off"
                    placeholder="请输入管理密码"
                    prefix-icon="el-icon-setting"
                    v-model="loginForm.password"></el-input>
        </el-form-item>
        <el-form-item class="btns">
          <!-- 点击进行登录提交 -->
          <el-button round
                     type="primary"
                     icon="el-icon-circle-check"
                     :loading=loading
                     @click="login">
            登 录
          </el-button>
          <!-- 点击重置表单 -->
          <el-button type="info"
                     round
                     icon="el-icon-circle-close"
                     @click="reset">

            重 置
          </el-button>
        </el-form-item>
        <el-form-item>
          <el-tooltip effect="dark"
                      content="暂未开放注册"
                      placement="top">
            <el-button type="text"
                       icon="el-icon-edit">注册用户</el-button>
          </el-tooltip>

          <el-tooltip effect="dark"
                      content="需提供原密码"
                      placement="top">
            <el-button type="text"
                       icon="el-icon-search"
                       @click="alertChangePwd">修改密码</el-button>
          </el-tooltip>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    const validPwd = function (rule, value, callback) {
      if (value.length > 5 && value.length < 15) {
        // const reg = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{5,15}$/
        const reg = /^([0-9]+){5,15}$/
        if (!reg.test(value)) {
          callback(new Error('密码必须且含有数字和字母'))
        } else {
          callback()
        }
      } else {
        callback(new Error('长度在 5 至 15 个字符之间'))
      }
    }
    return {
      loading: false,
      loginForm: {
        account: 'developer',
        password: '123456'
      },
      loginFormRules: {
        account: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          {
            min: 5,
            max: 15,
            message: '长度在 5 至 15 个字符之间',
            trigger: 'blur'
          }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'bulr' },
          { validator: validPwd, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    reset () {
      this.$refs.loginLoginRef.resetFields()
    },
    login () {
      this.$refs.loginLoginRef.validate(valid => {
        if (!valid) return
        const { account, password } = this.loginForm
        const _formParams = {
          account,
          password
        }
        this.loading = true
        this.$store.dispatch('doLogin', _formParams)
          .then(res => {
            this.loading = false
            this.$router.push('/')
          })
          .catch(() => {
            this.loading = false
          })
      })
    },
    alertChangePwd () { }
  }

}
</script>

<style lang="less" scoped>
.login_container {
  background-color: #2b4b6b;
  height: 100%;
}
.login_box {
  width: 450px;
  height: 300px;
  background-color: #fff;
  border-radius: 5px;
  position: absolute;
  left: 50%;
  top: 50%;
  //移动登录框至正中间
  transform: translate(-50%, -50%);

  .avatar_box {
    height: 130px;
    width: 130px;
    border-radius: 50%;
    padding: 10px;
    box-shadow: 0 0 10px #ddd;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: #fff;
    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
      background-color: #eee;
    }
  }
}
.login_form {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
}
.btns {
  display: flex;
  justify-content: flex-end;
}
</style>
